﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BOM.BaseData;
using BOM.dao;

namespace BOM.PriceBill
{
    public partial class PriceBillList : Form
    {
        public PriceBillList()
        {
            InitializeComponent();
        }
        //新增按钮
        private void btnAddNew_Click(object sender, EventArgs e)
        {
            try
            {
                PriceBillEntryForm.Rows = 0;//记下新建行的行数 没新增一行数据 就累加1
                PriceBillForm form = new PriceBillForm();
                CustomerList.BiaoJi = 2;
                form.Owner = this;
                PriceBillEntryForm.Rows = 0;
                form.ShowDialog();
            }
            catch
            { }
        }
        //查看按钮
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            try
            {
                CustomerList.XuanZID = Convert.ToInt32(dataGridView1.CurrentRow.Cells["id"].Value);
                CustomerList.BiaoJi = 0;
                PriceBillForm form = new PriceBillForm();
                form.Owner = this;
                form.ShowDialog();
            }
            catch
            { }

        }
        //编辑按钮
        private void btnEdit_Click(object sender, EventArgs e)
        {
            try
            {
                CustomerList.XuanZID = Convert.ToInt32(dataGridView1.CurrentRow.Cells["id"].Value);
                CustomerList.BiaoJi = 1;
                PriceBillForm form = new PriceBillForm();
                form.Owner = this;
                form.ShowDialog();
            }
            catch
            { }
        }
        //退出按钮
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //窗体的加载事件
        private void PriceBillList_Load(object sender, EventArgs e)
        {
            //设定列对齐方式
            dataGridView1.Columns["manageFee"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            dataGridView1.Columns["madeFee"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            dataGridView1.Columns["materialFee"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            dataGridView1.Columns["totalCost"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;

            //设定列的小数点格式
            dataGridView1.Columns["manageFee"].DefaultCellStyle.Format = "0.00";
            dataGridView1.Columns["madeFee"].DefaultCellStyle.Format = "0.00";
            dataGridView1.Columns["materialFee"].DefaultCellStyle.Format = "0.00";
            dataGridView1.Columns["totalCost"].DefaultCellStyle.Format = "0.00";

            //设定dataGridView1的列名只能是指定列名
            dataGridView1.AutoGenerateColumns = false; 
            LoadingDataGridView1();
        }
        //加载dataGridView1中的数据
        public void LoadingDataGridView1()
        {
            DataTable table = PriceBillNew.SelectPriceBill();
            dataGridView1.DataSource = table;

        }
        //删除按钮
        private void btnRemove_Click(object sender, EventArgs e)
        {
             DialogResult dr = MessageBox.Show("您确定要删除吗？", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
             if (dr == DialogResult.OK)
             {
                 try
                 {
                     CustomerList.XuanZID = Convert.ToInt32(dataGridView1.CurrentRow.Cells["id"].Value);
                     //MessageBox.Show(CustomerList.XuanZID.ToString());
                     bool bol = PriceBillNew.DeleteFidPriceBill(CustomerList.XuanZID);
                     if (bol == true)
                     {
                         bool bl = PriceBillNew.DeletePriceBillEntry(CustomerList.XuanZID);
                         LoadingDataGridView1();
                         MessageBox.Show("删除成功", "提示");
                     }
                     else
                     {
                         MessageBox.Show("删除失败", "提示");
                     }
                 }
                 catch
                 { }
             }
        }
        //刷新按钮
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;
            LoadingDataGridView1();
        }
        //双击某行时发生
        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                CustomerList.XuanZID = Convert.ToInt32(dataGridView1.CurrentRow.Cells["id"].Value);
                CustomerList.BiaoJi = 1;
                PriceBillForm form = new PriceBillForm();
                form.Owner = this;
                form.ShowDialog();
            }
            catch(Exception exp)
            {
                MessageBox.Show(exp.Message);
            }
        }
        //搜索按钮
        private void btnSearch_Click(object sender, EventArgs e)
        {
            string str = txtSearch.Text;
            DataTable table = PriceBillNew.SelectPriceBillNberNme(str);
            dataGridView1.DataSource = table;
        }
    }
}
